import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { tokenToString } from 'typescript'
import type { Token } from '@/api/auth/types'
export const useTokenStore = defineStore("token", () => {
    const tokenInfo = ref<Token>({
        username: '',
        token: ''
    })


    const setToken = (token: Token) => {
        tokenInfo.value = token
    }

    /* 清空token */
    const clearToken = () => {
        tokenInfo.value = {
            username: '',
            token: ''
        }
    }

    /* getter */
    const getUserName = computed(() => tokenInfo.value.username)

    const getToken = computed(() => tokenInfo.value.token)

    return { tokenInfo, setToken, clearToken, getUserName, getToken }
}, {
    persist: {
        
        key: "tokenStore",
        // 默认是 localStorage
        storage: sessionStorage,
    }
})